﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace snat.model {
    public class TimeWindowedNode : WindowedNode {

        public event windowDelegate NewWindow;

        /// <summary>
        /// Window size, DEFAULT : One Day
        /// </summary>
        public static TimeSpan WindowSize { get; private set; }
        private static DateTime WindowStart;
        private static object registrationLock = new object();
        private static bool registered = false;

        public TimeWindowedNode(int _id, string _name)
            : base(_id, _name) {
                lock (registrationLock) {
                    if (!registered) {
                        TimeWindowedNode.WindowSize = new TimeSpan(1, 0, 0, 0);
                        NetworkModel.Instance.TapeLoaded += OnNetworkModelLoaded;
                        registered = true;
                    }
                }

        }

        private void OnNetworkModelLoaded() {
            TimeWindowedNode.WindowStart = NetworkModel.Instance.StartTime;
        }

        protected override void OnMessageProcessed(Message m) {
            while (m.Timestamp >= WindowStart + WindowSize) {
                WindowStart += WindowSize;
                TriggerOverlayReset();
                NewWindow();
            }
        }
    }
}
